﻿#include "TCMS.h"
#include <qtreewidget.h>
#include <QHBoxLayout>
#include <qstringlist.h>
#include <qgroupbox.h>
#include <qgridlayout.h>
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qmessagebox.h>
#include <qdebug.h>

#include<Windows.h>
#include <qfile.h>
#include <QtXml>
#include <QDomDocument>

#include <qstylefactory.h>
#include <qfiledialog.h>
#include <QToolBar>

#include <QDesktopServices>
#include <QRect>
#include <QRect>
#include <qstackedwidget.h>

#include <QCoreApplication>
#include "ddumsgtest.h"
#include"netconfigform.h"
#include"formmvb.h"
#include"formeke.h"
#include"sideWidget.h"
#pragma execution_character_set("utf-8")
TCMS::TCMS(QWidget* parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);


    InitUI();
    ConnectSinalsAndSlots();

    ui.plainTextEdit->onLog("软件启动....");
    //更新配置到其他窗口
    netconfigform->ReadCfg();

    InitStatus();
    showMaximized();
}

TCMS::~TCMS()
{
    delete dduMsgTest;
    delete netconfigform;
    delete formMvb;
    delete formEKE;

}

void TCMS::ConnectSinalsAndSlots()
{
    connect(ui.actionTRDPTest, &QAction::triggered, this, &TCMS::ShowDDUTest);
    connect(ui.actionNetCfg, &QAction::triggered, this, &TCMS::ShowNetCfg);
    connect(netconfigform, &NetConfigForm::CfgChanged, this, &TCMS::OnConfigChanged);
    connect(dduMsgTest, &DDUMsgTest::SendLogMsg, ui.plainTextEdit, &QPlainTextEitLog::onLog);
    connect(formMvb, &FormMvb::SendLogMsg, ui.plainTextEdit, &QPlainTextEitLog::onLog);
    connect(netconfigform, &NetConfigForm::SendLogMsg, ui.plainTextEdit, &QPlainTextEitLog::onLog);
    connect(formEKE, &FormEKE::SendLogMsg, ui.plainTextEdit, &QPlainTextEitLog::onLog);
    connect(ui.action_log,&QAction::triggered, this, &TCMS::on_actionLogFile_triggered);
    connect(ui.action_userManager,&QAction::triggered, this, &TCMS::showUserManager);

}

void TCMS::InitUI()
{
    setWindowTitle("TCMS自动测试");
    this->setWindowIcon(QIcon(":/src/icon/icon_64/train.svg"));
    ui.actionTestRecord->setIcon(QIcon(":/src/icon/TestPassed.png"));

    dduMsgTest = new DDUMsgTest(this);
    netconfigform = new NetConfigForm(this);
    formMvb = new FormMvb(this);
    formEKE = new FormEKE(this);
    m_formManager = new UserManagerForm(this);

    ui.stackedWidget->addWidget(dduMsgTest);
    ui.stackedWidget->addWidget(netconfigform);
    ui.stackedWidget->addWidget(formMvb);
    ui.stackedWidget->addWidget(formEKE);
    ui.stackedWidget->addWidget(m_formManager);

    ui.stackedWidget->setCurrentWidget(dduMsgTest);
    ui.actionShowLogControl->setChecked(true);

    m_sideWidget = new sideWidget(this);

    m_propertyAnimation = new QPropertyAnimation(m_sideWidget,"geometry");
    m_propertyAnimation->setEasingCurve(QEasingCurve::InOutSine);
    m_propertyAnimation->setDuration(400);
}


void TCMS::InitStatus()
{
//     初始化状态栏
    QLabel* label = new QLabel("自动测试软件v1.0");
    ui.statusbar->addPermanentWidget(label);

    m_UserName = new QLabel("当前用户:admin",this);
    m_UserName->setMinimumWidth(250);
    m_UserLevel=new QLabel("管理员",this);
    // 将初始化的标签添加到底部状态栏上
    ui.statusbar->addWidget(m_UserName);
    ui.statusbar->addWidget(m_UserLevel);

}


void TCMS::updateStatus(const UserInfo &userinfo)
{
    m_UserName->setText("当前用户:"+userinfo.name());
    m_UserLevel->setText("级别:"+userinfo.OperatorLevel());
}

void TCMS::initStartPos()
{

}

void TCMS::resizeEvent(QResizeEvent *event)
{
   m_sideWidget->setFixedSize(500,this->height());
   if(!m_bSideflag)
   {
        m_sideWidget->move(this->rect().right(),0);// 右侧停靠
   }
   else
   {
        //如果显示 则进行隐藏
        on_actionTestRecord_triggered();
   }
}

void TCMS::showEvent(QShowEvent *event)
{
    QMainWindow::showEvent(event);
    initStartPos();
}

void TCMS::CreateToolBar()
{
}

void TCMS::SetRecentUser(const UserInfo &userInfo)
{
    m_recentUser = userInfo;
    updateStatus(m_recentUser);

}

void TCMS::ShowEditTrdp()
{
//    ui.stackedWidget->setCurrentWidget(EditTrdp);
}

void TCMS::ShowDDUTest()
{
    ui.stackedWidget->setCurrentWidget(dduMsgTest);
}

void TCMS::ShowNetCfg()
{
    ui.stackedWidget->setCurrentWidget(netconfigform);

}

void TCMS::ShowFormMVB()
{
    ui.stackedWidget->setCurrentWidget(formMvb);
}

void TCMS::ShowFormEKE()
{
    ui.stackedWidget->setCurrentWidget(formEKE);
}

void TCMS::ShowLogControl()
{

}

void TCMS::showUserManager()
{
     ui.stackedWidget->setCurrentWidget(m_formManager);
}


void TCMS::RunFtpTool()
{

    QString applicationPath = QApplication::applicationDirPath()+"/ftptool/main.exe";
    QFile file(applicationPath);
   if(!file.exists())
    {
        QMessageBox::information(this,"tip","未找到文件:"+applicationPath);

    }
    //执行程序
    QProcess program;
    QString cmd = applicationPath;
    program.start(cmd);
    program.waitForFinished(-1);



}

//配置文件改变，将对应配置内容更新到其他窗口
void TCMS::OnConfigChanged()
{
    qDebug()<<"value change"<<endl;

}

void TCMS::on_actionShowLogControl_triggered()
{
    bool bVisible = ui.plainTextEdit->isVisible();

    ui.plainTextEdit->setVisible(!bVisible);
    ui.actionShowLogControl->setChecked(!bVisible);

}


void TCMS::on_actionmvb_triggered()
{
    ShowFormMVB();
}


void TCMS::on_actionEKE_triggered()
{
    ShowFormEKE();
}


void TCMS::on_actionFtpTool_triggered()
{
//    RunFtpTool();
}

void TCMS::closeEvent(QCloseEvent *event)
{
    // 弹出对话框询问用户是否真的想要退出
    QMessageBox::StandardButton resBtn = QMessageBox::question(this, "退出确认",
                                                                   "是否退出?",
                                                               QMessageBox::No | QMessageBox::Yes,
                                                               QMessageBox::No);

    if (resBtn != QMessageBox::Yes)
    {
        event->ignore();  // 如果用户选择不退出，忽略关闭事件
    } else
    {
        event->accept();  // 如果用户选择退出，接受关闭事件
    }
}


void TCMS::on_actionLogFile_triggered()
{
    // 将文件夹路径转换为QUrl对象
    QUrl folderUrl = QUrl::fromLocalFile("./LogFiles/INFO");

    // 打开文件夹
    if (!QDesktopServices::openUrl(folderUrl))
    {
        // 如果打开失败，可以在这里处理错误
        qWarning("无法打开文件夹: %s", qPrintable(folderUrl.path()));
    }
}


void TCMS::on_actionImgSavePath_triggered()
{
    // 将文件夹路径转换为QUrl对象
    QUrl folderUrl = QUrl::fromLocalFile(ConfigData::imgSavePath);

    // 打开文件夹
    if (!QDesktopServices::openUrl(folderUrl))
    {
        // 如果打开失败，可以在这里处理错误
        qWarning("无法打开文件夹: %s", qPrintable(folderUrl.path()));
    }
}

void TCMS::slotShowAvatarLabel()
{
    m_UserName->setText("当前用户:"+m_recentUser.name());
}


void TCMS::on_actionTestRecord_triggered()
{
    QRect startPos=QRect(this->rect().right(),0,m_sideWidget->width(),m_sideWidget->height());
    QRect EndPos=QRect(this->rect().right()-m_sideWidget->width()+10,0,m_sideWidget->width(),m_sideWidget->height());
    //显示侧边栏
    if(!m_bSideflag)
    {

        m_propertyAnimation->setStartValue(startPos);
        m_propertyAnimation->setEndValue(EndPos);
        m_propertyAnimation->start();
        m_bSideflag = !m_bSideflag;
    }
    else
    {
        m_propertyAnimation->setStartValue(EndPos);
        m_propertyAnimation->setEndValue(startPos);
        m_propertyAnimation->start();
        m_bSideflag = !m_bSideflag;
    }
}

